//
// AWS Manager - A free AWS console application demonstrating advanced concepts
// Copyright (C) 2008-2010 Sean O'Dell. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.awsmanager.common;

import java.util.Map;
import java.util.Properties;
import org.jets3t.service.Jets3tProperties;
import org.jets3t.service.S3Service;
import org.jets3t.service.impl.rest.httpclient.RestS3Service;
import org.jets3t.service.security.AWSCredentials;

/**
 *
 * @author sodell
 */
public class S3StorageConnection {

    private S3Service s3Service = null;
    private String statusmsg = null;

    public S3StorageConnection(Properties awsProps, Map s3ServerInfo) {
        connect(awsProps, s3ServerInfo);
    }

    public S3Service getS3Service() {
        return s3Service;
    }

    public void connect(Properties awsProps, Map s3ServerInfo) {

        Jets3tProperties jets3tProperties = new Jets3tProperties();

        if ((awsProps.getProperty("aws.accessKey").equals(""))
                || (awsProps.getProperty("aws.secretKey").equals(""))
                || (awsProps.getProperty("aws.s3server").equals(""))) {
            this.statusmsg = "Cannot connect to S3 server, keys not supplied correctly.";
            return;
        }

        S3StorageServer s3StorageServer = (S3StorageServer) s3ServerInfo.get(awsProps.getProperty("aws.s3server"));
        jets3tProperties.setProperty("s3service.s3-endpoint", s3StorageServer.getS3Server());

        if (s3StorageServer.getS3ServerPort() > 0) {
            jets3tProperties.setProperty("s3service.s3-endpoint-http-port", String.valueOf(s3StorageServer.getS3ServerPort()));
        }
        if (s3StorageServer.getS3ResourcePrefix() != null) {
            jets3tProperties.setProperty("s3service.s3-endpoint-virtual-path", s3StorageServer.getS3ResourcePrefix());
        }

        if (s3StorageServer.getS3serverProxyHost() != null) {
            jets3tProperties.setProperty("httpclient.proxy-autodetect", "false");
            jets3tProperties.setProperty("httpclient.proxy-host", s3StorageServer.getS3serverProxyHost());
        }

        if (s3StorageServer.getS3ServerPort() > 0) {
            jets3tProperties.setProperty("httpclient.proxy-port", String.valueOf(s3StorageServer.getS3serverProxyPort()));
        }

        jets3tProperties.setProperty("s3service.https-only", String.valueOf(s3StorageServer.isS3HTTPSonly()));
        jets3tProperties.setProperty("s3service.disable-dns-buckets", String.valueOf(s3StorageServer.isS3DisableDNSbuckets()));

        try {
            AWSCredentials awsCredentials = new AWSCredentials(awsProps.getProperty("aws.accessKey"),
                    awsProps.getProperty("aws.secretKey"));
            this.s3Service = new RestS3Service(awsCredentials, "OPC Manager", null, jets3tProperties);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
